package com.cmccgz.udacs_landlord_webapp.main.unlock;


import android.os.Environment;
import android.support.annotation.NonNull;

import com.cmccgz.udacs_landlord_webapp.base.ApiService;
import com.cmccgz.udacs_landlord_webapp.bean.RecordDetail;
import com.cmccgz.udacs_landlord_webapp.bean.ResultBean;
import com.cmccgz.udacs_landlord_webapp.update.StringUtils;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import rx.Subscriber;

public class RecordDetailPresenter implements RecordDetailContract.Presenter {
    private RecordDetailContract.View view;

    public RecordDetailPresenter(RecordDetailContract.View view) {
        this.view = view;
    }

    @Override
    public void getDetail(String id) {
        ApiService.getRecDetail(id).subscribe(new Subscriber<ResultBean<RecordDetail>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                view.getDetailError("获取记录详情失败！");
            }

            @Override
            public void onNext(ResultBean<RecordDetail> recordDetailResultBean) {
                if ("0".equals(recordDetailResultBean.rsmsg.rscode)&&recordDetailResultBean.resp!=null) {
                    view.getDetailSuccess(recordDetailResultBean.resp);
                }else{
                    view.getDetailError(recordDetailResultBean.rsmsg.rsdesc);
                }
            }
        });
    }

    @Override
    public void getVideoUrl(String webUrl) {
        final File file = new File(Environment.getExternalStorageDirectory().getPath() + File.separator + "cmccgz" + File.separator + "vCache", getFileName(webUrl));
        if (file.exists() && file.length() != 0) {
            view.getVideoUrlSuccess(file.getAbsolutePath());
            return;
        }
        ApiService.downFile(webUrl,file).subscribe(new Subscriber<Boolean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                view.getVideoUrlError("加载视频失败!");
            }

            @Override
            public void onNext(Boolean aBoolean) {
                if (!aBoolean||!file.exists()) {
                   onError(null);
                    return;
                }
                if (file.length()==0) {
                    view.getVideoUrlError("该视频为空，请稍后再试!");
                    return;
                }
                view.getVideoUrlSuccess(file.getAbsolutePath());
            }
        });
    }

    @NonNull
    private String getFileName(String videoUrl,String endName) {
        int index = videoUrl.lastIndexOf("/");
        Pattern p = Pattern.compile("[0-9a-zA-Z]+");
        String substring = videoUrl.substring(index);
        Matcher matcher = p.matcher(substring);
        String fileName = "";
        while (matcher.find()) {
            fileName += matcher.group();
        }
        endName=StringUtils.isEmpty(endName)?"":("."+endName);
        return fileName +endName;
    }
    @NonNull
    private String getFileName(String videoUrl) {
        return getFileName(videoUrl,null) ;
    }
}
